package com.copa.l2383;

import java.util.Arrays;

/**
 * @author copa
 * @createDate 2023-03-13 8:08
 * @function 二次遍历法
 */
public class Test1 {

    public int minNumberOfHours(int initialEnergy, int initialExperience, int[] energy, int[] experience) {
        int result = 0;
        int energySum = Arrays.stream(energy).sum();
        result = initialEnergy <= energySum ? energySum - initialEnergy + 1 : result;
        for (int i : experience) {
            if (i >= initialExperience) {
                int diff = i - initialExperience + 1;
                initialExperience += diff;
                result += diff;
            }
            initialExperience += i;
        }
        return result;
    }

    public static void main(String[] args) {
        int initialEnergy = 1;
        int initialExperience = 1;
        int[] energy = {1, 1, 1, 1};
        int[] experience = {1, 1, 1, 50};
        System.out.println(new Test1().minNumberOfHours(initialEnergy, initialExperience, energy, experience));
    }
}
